<template>
  <div>
    <el-page-header icon="" title="高校教学考试成绩管理系统">
      <template #content>
        <span class="text-large font-600 mr-3">个人中心</span>
      </template>
    </el-page-header>

    <el-row :gutter="20">
      <!-- 左 -->
      <el-col :span="8">
        <el-card class="box-card">
          <el-avatar :size="100" :src="avatarUrl" />
          <h3>{{ store.state.userInfo.name }}</h3>
          <h3>{{ roleText }}</h3>
          <hr />
          <div>工号：{{ store.state.userInfo.username }} <br />性别：{{ store.state.userInfo.gender === 1 ? '男' : '女' }}</div>
        </el-card>
        <el-card style="margin-top: 20px; padding: 21px" v-if="store.state.userInfo.role === 3">
          <div>
            专业：{{ studentData.major }} <br />
            班级：{{ studentData.classes }}<br />
            入学年份：{{ studentData.year }}
          </div>
        </el-card>
      </el-col>

      <!-- 右 -->
      <el-col :span="16">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>个人信息</span>
            </div>
          </template>
          <el-form ref="userFormRef" :model="userForm" :rules="userFormRules" label-width="80px" class="demo-ruleForm" status-icon>
            <el-form-item label="电话" prop="phone">
              <el-input v-model.number="userForm.phone" />
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="userForm.email" />
            </el-form-item>
            <el-form-item label="头像" prop="avatar">
              <Upload :avatar="userForm.avatar" @uploadchange="handleAvatarChange" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm()">更新</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { useStore } from 'vuex'
import { computed, ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import upload from '@/util/upload'
import Upload from '@/components/upload/Upload.vue'
import axios from 'axios'
const store = useStore()
const avatarUrl = computed(() => (store.state.userInfo.avatar ? 'http://localhost:3000' + store.state.userInfo.avatar : 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'))
const roleText = computed(() => {
  if (store.state.userInfo.role === 1) {
    return '管理员'
  } else if (store.state.userInfo.role === 2) {
    return '教师'
  } else {
    return '学生'
  }
})
const userFormRef = ref()
const { phone, email, avatar } = store.state.userInfo
const userForm = reactive({
  phone,
  email,
  avatar, //本地使用
  file: null //传到后端
})
const userFormRules = reactive({
  phone: [
    { required: true, message: '请输入电话', trigger: 'blur' },
    { type: 'number', message: '电话应是数字' }
  ],
  email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
  avatar: [{ required: true, message: '请上传头像', trigger: 'blur' }]
})
const studentData = ref({})
onMounted(async () => {
  if (store.state.userInfo.role === 3) {
    const res = await axios.get(`/api/student/list/${store.state.userInfo.username}`)
    studentData.value = res.data.data[0]
  }
})
// 选择完头像回调
const handleAvatarChange = (file) => {
  // console.log(file.raw)
  userForm.avatar = URL.createObjectURL(file)
  userForm.file = file
}
const submitForm = () => {
  userFormRef.value.validate(async (valid) => {
    if (valid) {
      // console.log(userForm)
      const res = await upload('/api/user/upload', userForm)
      // console.log(123)
      // console.log(res.data)
      if (res.data.ActionType === 'OK') {
        store.commit('changeUserInfo', res.data.data)
        ElMessage.success('更新成功！')
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.el-row {
  margin-top: 20px;
  .box-card {
    text-align: center;
    padding: 21px;
  }
}
</style>
